Imports System
Imports Telerik.WebControls

Partial Class MasterPrivateEval
    Inherits System.Web.UI.MasterPage

    Private InitialPage As String

    Protected Sub lnkCastellano_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCastellano.Click
        Session("selectedLanguage") = "es-ES"
        Response.Redirect(Request.Url.OriginalString)
    End Sub

    Protected Sub lnkCatala_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCatala.Click
        Session("selectedLanguage") = "ca-ES"
        Response.Redirect(Request.Url.OriginalString)
    End Sub

    Protected Sub LoginStatus1_LoggingOut(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles LoginStatus1.LoggingOut
        InitialPage = "~/eval/index.aspx"
    End Sub

    Protected Sub LoginStatus1_LoggedOut(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginStatus1.LoggedOut
        Session.Clear()
        Me.Response.Redirect("~/eval/index.aspx")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Roles.IsUserInRole(Membership.GetUser().UserName, "AdministradoresPortal") Then
            Me.lblClienteName.Visible = False
            Me.lblCliente.Visible = False
        Else
            'Obtenemos el Cliente
            If Me.Session("CustomerName") Is Nothing Or Session("IndCalcCompetencias") Is Nothing Then
                Me.CheckProfile()
                Dim tadcliente As New EVITableAdapters.ClientesTableAdapter
                Dim _cliente As New EVI.ClientesDataTable
                _cliente = tadcliente.GetDataByIdCliente(Profile("Cliente"))
                If _cliente.Rows.Count > 0 Then
                    Session("CustomerName") = _cliente(0).NombreCliente
                    Session("IndCalcCompetencias") = _cliente(0).IndCalcCompetencias
                End If
                Response.Redirect(Request.Url.OriginalString)
            End If
            Me.lblClienteName.Text = Me.Session("CustomerName")
            If Roles.IsUserInRole(Membership.GetUser().UserName, "evaluadores") Then
                'Activamos los puntos del PanelBar a los que tiene acceso un evaluador
                Me.PanelBarMenuAdmin.FindItemByValue("MisEvaluaciones").Visible = True
                Me.PanelBarMenuAdmin.FindItemByValue("AyudaEvaluador").Visible = True
            End If
            If Roles.IsUserInRole(Membership.GetUser().UserName, "rrhh") Then
                'Activamos los puntos del panelBar a los que tiene acceso un coordinador.
                'Me.PanelBarMenuAdmin.FindItemByValue("EvaluacionesPersonal").Visible = True
                Me.PanelBarMenuAdmin.FindItemByValue("Configuracion").Visible = True
                Me.PanelBarMenuAdmin.FindItemByValue("AyudaCoordinador").Visible = True
                'Desactivo la ayuda del evaluador, ya que es posible tener usuarios que sean
                ' a la vez evaluadores y coordinadores.
                Me.PanelBarMenuAdmin.FindItemByValue("AyudaEvaluador").Visible = False
            End If
        End If
        If Session("selectedLanguage") = "ca-ES" Then
            lnkCatala.Enabled = False
            lnkCastellano.Enabled = True
        End If

        If Session("selectedLanguage") = "es-ES" Then
            lnkCastellano.Enabled = False
            lnkCatala.Enabled = True
        End If

        'Activar el punto del panel bar activo
        Dim clickedItem As RadPanelItem = PanelBarMenuAdmin.FindItemByUrl(Request.Url.PathAndQuery)

        If Not (clickedItem Is Nothing) Then
            ShowPathPanelBar(clickedItem)
        End If

        'Cargar el logo del cliente
        Dim tabClientes As EVI.ClientesDataTable
        Using tadClientes As New EVITableAdapters.ClientesTableAdapter()
            tabClientes = tadClientes.GetDataByIdCliente(New Guid(Profile("cliente").ToString))
            If tabClientes.Count > 0 Then
                Me.imgLogoCliente.ImageUrl = tabClientes(0).Logotipo.ToString
            Else
                Me.imgLogoCliente.ImageUrl = "../images/upload/blank.gif"
            End If
        End Using
    End Sub

    Private Sub CheckProfile()
        Dim tadclientesusuarios As New EVITableAdapters.ClientesUsuariosTableAdapter
        Dim tableclienteusuarios As EVI.ClientesUsuariosDataTable

        tableclienteusuarios = tadclientesusuarios.GetDataByUserId(Membership.GetUser().UserName)
        If tableclienteusuarios.Rows.Count > 0 Then
            Profile("Cliente") = tableclienteusuarios.Rows(0)("idCliente")

            Dim tadCliente As New EVITableAdapters.ClientesTableAdapter
            Dim tableCliente As EVI.ClientesDataTable
            tableCliente = tadCliente.GetDataByIdCliente(tableclienteusuarios.Rows(0)("idCliente"))
            If tableCliente.Rows.Count > 0 Then
                Profile("IdiomaCliente") = tableCliente.Rows(0)("ididioma")
                If Profile("IdiomaCliente").ToString = "e4915e24-3016-4a23-b67b-af87f152ea28" Then
                    Session("selectedLanguage") = "ca-ES"
                Else
                    Session("selectedLanguage") = "es-ES"
                End If
            End If
            Profile.Save()
        End If
    End Sub

    Private Sub ShowPathPanelBar(ByVal clickedItem As RadPanelItem)
        Dim childItem As RadPanelItem
        For Each childItem In clickedItem.Panelbar.GetAllItems()
            childItem.CssClass = ""
        Next childItem

        clickedItem.CssClass = "selected"

        Dim item As RadPanelItem = clickedItem
        Dim i As Integer
        For i = 0 To clickedItem.Level - 1
            item = CType(item.Parent, RadPanelItem)
            clickedItem.Panelbar.FindItemByText(item.Text).CssClass = "selected"
        Next i
        item.Expanded = True
    End Sub

End Class

